package com.mmall.controller;

import com.mmall.common.ApplicationContextHelper;
import com.mmall.common.JsonData;
import com.mmall.dao.SysAclModuleMapper;
import com.mmall.exception.PermissionException;
import com.mmall.model.SysAclModule;
import com.mmall.param.TestVo;
import com.mmall.util.BeanValidator;
import com.mmall.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by megan on 2018/1/21.
 */
@Controller
@RequestMapping("/test/")
@Slf4j
public class TestController {

    @RequestMapping("hello.json")
    @ResponseBody
    public JsonData test(){
        log.info("hello");
        throw new RuntimeException("test exception");
        //return JsonData.success("hello permission");
    }


    @RequestMapping("validate.json")
    @ResponseBody
    public JsonData validate(TestVo vo){
        log.info("hello");
        try {
            Map<String,String> map = BeanValidator.validateObject(vo);
            if(MapUtils.isNotEmpty(map)){
               for (Map.Entry<String,String> entry : map.entrySet()){
                    log.info("{}--->{}",entry.getKey(),entry.getValue());
               }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  JsonData.success("test validate");
    }

    @RequestMapping("applicationContextHelper.json")
    @ResponseBody
    public Object applicationContextHelper(TestVo vo){
        log.info("#################");
        SysAclModuleMapper moduleMapper = ApplicationContextHelper.popBean(SysAclModuleMapper.class);
        SysAclModule sysAclModule = moduleMapper.selectByPrimaryKey(1);
        log.info("#################");
        Object obj2String =JsonMapper.obj2String(sysAclModule);
        log.info("obj2String:{}",obj2String);
        return  obj2String;
    }


}
